package com.weiqk.pmdemo.service.impl;

import com.weiqk.pmdemo.dao.PayLinkRepostitory;
import com.weiqk.pmdemo.dao.PurchaseLinkRespository;
import com.weiqk.pmdemo.dao.PurchaseRepository;
import com.weiqk.pmdemo.dto.PayLinkInDto;
import com.weiqk.pmdemo.pojo.ApplyLink;
import com.weiqk.pmdemo.pojo.PayLink;
import com.weiqk.pmdemo.pojo.Project;
import com.weiqk.pmdemo.pojo.Purchase;
import com.weiqk.pmdemo.service.PayLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class PayLinkServiceImpl implements PayLinkService {
    @Autowired
    PayLinkRepostitory payLinkRepostitory;
    @Autowired
    PurchaseRepository purchaseRepository;

    @Override
    public Iterable<PayLink> findAll() {
        Iterable<PayLink> payLinks = payLinkRepostitory.findAll();
        return payLinks;
    }

    @Override
    public Boolean deleteById(int id) {
        payLinkRepostitory.deleteById(id);
        return true;
    }

    @Override
    public Boolean update(PayLinkInDto payLinkInDto) {
        //处理时间
        PayLink payLink = payLinkInDto.getPayLink();
        payLink.setUpadteTime(new Date());
        //处理外键数据
        int purchaseNum = payLinkInDto.getPurchaseNum();
        Purchase purchase = purchaseRepository.findOne(purchaseNum);
        payLink.setPurchase(purchase);

        //操作数据库
        payLinkRepostitory.save(payLink);
        return true;
    }

    @Override
    public Boolean put(PayLinkInDto payLinkInDto) {
        //处理时间
        PayLink payLink = payLinkInDto.getPayLink();
        payLink.setUpadteTime(new Date());
        payLink.setCreateTime(new Date());
        //处理外键数据
        int purchaseNum = payLinkInDto.getPurchaseNum();
       Purchase purchase = purchaseRepository.findOne(purchaseNum);
       payLink.setPurchase(purchase);

        //操作数据库
        payLinkRepostitory.save(payLink);
        return true;
    }
}
